import Vue from 'vue'
import Vuex from 'vuex';
import actions from './actions.js';
import {
  getJson,
  contains
} from './utils.js';

Vue.use(Vuex)

const state = {
  userInfo: {},
  videoInfo: {}
}

const mutations = {
  saveUser(state, user) {
    state.userInfo = user;
  },
  saveVideoInfo(state, data) {
    state.videoInfo = data;
  },
}

const getters = {
  userInfo: (state) => {
    let userInfo;
    if (state.userInfo.id) {
      userInfo = state.userInfo;
    } else {
      if (contains('userInfo')) {
        userInfo = state.userInfo = getJson('userInfo')
      }
    }
    return userInfo;
  },
  videoInfo: (state) => {
    let videoInfo;
    if (state.videoInfo.id) {
      videoInfo = state.videoInfo;
    } else {
      if (contains('videoInfo')) {
        videoInfo = state.videoInfo = getJson('videoInfo')
      }
    }
    return videoInfo;
  }
}
export default new Vuex.Store({
  state,
  actions,
  getters,
  mutations
})
